clear all
clear matrix
clear mata

set more off

local path = "...folder name"

cd "`path'"

*****************************
*Estimation for steel making*
*****************************

local dlwcprogram = "acf_mata_steel.do"

do `dlwcprogram'

use steel_plant_pf.dta, clear

/*generate log variables*/
gen y = log(production)
gen l = log(labor)
gen k = log(size)
gen logscrap = log(tscrap/1000) 
gen p = logscrap /*use scrap steel as the proxy variable*/
gen age = year - birth
gen m = logiron

gen dsecond = 0
replace dsecond = 1 if second > 0 & second < = 100

*trim outliers
drop if labor < 1 | labor > 500000

*---------------creating variables---------------------------------------------*
* higher order terms on inputs k l a p

forvalues ll = 0(1)5{
         local kmax = 5 - `ll'
		 forvalues kk = 0(1)`kmax'{
		      local amax = 5 - `ll' - `kk'
			  forvalues aa = 0(1)`amax'{
			       local mmax = 5 - `ll' - `kk' - `aa'
				   forvalues mm = 0(1)`mmax'{
				    local pmax = 5 - `ll' - `kk' - `mm' - `aa'
					forvalues pp = 0(1)`pmax'{
				    gen poly`ll'`kk'`mm'`aa'`pp' = (l^`ll')*(k^`kk')*(m^`mm')*(age^`aa')*(p^`pp')
				     }
				   }
			  }
		 }
}


*------FIRST STAGE USING EXP AS INPUT  ----------------------------------------*
local regphi = "poly* dsecond second i.t i.owner i.region"

xi: reg y `regphi'
predict phi
predict epsilon, res
label var phi "phi_it 
label var epsilon "measurement error first stage

local regx = "l k m age i.t i.owner i.region"

xi: reg y `regx' if !missing(p)
for any l k m: gen OLSX=_b[X]
gen OLSConst=_b[_c]	
gen OLSa = _b[age]

*-------------------generate ols estimate in Table 4 column (5)----------------*
local regx = "l k m age i.t i.owner i.region"

xi: reg y `regx' if !missing(p)
outreg2 using steel_ols_lmk, keep(l k m age) nocons addtext(Ownership FE,YES, Province FE,YES, Time FE, YES) tex replace
*------------------------------------------------------------------------------*
xtset id time, monthly
gen phi_lag=L.phi
gen l_lag=L.l
gen k_lag=L.k
gen age_lag = L.age
gen m_lag = L.m
gen dsecond_lag = L.dsecond
gen second_lag = L.second
sort id t
gen const=1
drop if y==.
drop if l_lag==.
drop if l == .
drop if k==.
drop if m ==.
drop if m_lag == .
drop if phi==.
drop if phi_lag==.
drop if age ==.


*-------------------------------start search-----------------------------------*
*try ols initial value first

for any l k m: gen initialX=OLSX
gen initiala = OLSa
gen initialx = sqrt(initiall/(1-initiall))
gen initialz = sqrt(initialk/(1-initialk))

dlwc

matrix result = (beta_dlw[1,1]^2/(1+beta_dlw[1,1]^2),beta_dlw[1,2]^2/(1+beta_dlw[1,2]^2),beta_dlw[1,3],beta_dlw[1,4],beta_dlw[1,5],initiall[1],initialk[1],initialm[1])

putexcel A1 = mat(result) using acf_steel_lkm_link, sheet("iron_scrap") modify

local n = 2
local iter = 101

set seed 12345 

while `n' < = `iter'{

gen u = (1.2 - 0.8)*runiform() + 0.8 /*search around 80% to 120% of ols coefficients*/

forvalues i = 1(1)4{
gen u`i' = u[`i']
}

replace initiall = OLSl*u1
replace initialx = sqrt(initiall/(1-initiall))
replace initialk = OLSk*u2
replace initialz = sqrt(initialk/(1-initialk))
replace initialm = OLSm*u3
replace initiala = OLSa*u4


dlwc

matrix result = (beta_dlw[1,1]^2/(1+beta_dlw[1,1]^2),beta_dlw[1,2]^2/(1+beta_dlw[1,2]^2),beta_dlw[1,3],beta_dlw[1,4],beta_dlw[1,5],initiall[1],initialk[1],initialm[1])


putexcel A`n' = mat(result) using acf_steel_lkm_link, sheet("iron_scrap") modify

matrix drop result
drop u u1-u4

local n = `n' + 1
}

/*generate estimates in Table 4 column (6)*/
import excel "...folder name\acf_steel_lkm_link.xlsx", sheet("iron_scrap") clear
sort E
keep if _n == 1
rename A beta_l
rename B beta_k
rename C beta_m
rename D beta_a
drop E
save beta_steel_link.dta, replace
export excel using steel_acf_lkm_link.xlsx, sheet("iron_scrap") replace 

/*generate tfp estimates*/
use steel_plant_pf.dta, clear

/*generate log variables*/
gen y = log(production)
gen l = log(labor)
gen k = log(size)
gen logiron = log(tpigiron/1000)
gen age = year - birth
gen m = logiron

gen beta_l = .
gen beta_k = .
gen beta_m = .
gen beta_a = .

append using beta_steel_link.dta

replace beta_l = beta_l[_N]
replace beta_k = beta_k[_N]
replace beta_m = beta_m[_N]
replace beta_a = beta_a[_N]
drop if _n == _N

/*generate tfp estimates*/
gen tfp = y - beta_l*l - beta_k*k - beta_m*m - beta_a*age

*trim outliers
drop if labor < 1 | labor > 500000

egen mean_tfp = mean(tfp)
replace tfp = exp(tfp - mean_tfp)
gen w_det_steel = exp(beta_l*l + beta_k*k + beta_m*m + beta_a*age) /*generate weight using the deterministic part of the production function*/

keep plant firm time year tfp w_det_steel k l
rename tfp tfp_steel

gen t = string(time)
gen fid = plant + t
drop t 

save steel_tfp_lmk_link.dta, replace

/*generate controls for upstream*/
use steel_plant_pf.dta, clear

/*generate log variables*/
gen y = log(production)
gen l = log(labor)
gen k = log(size)
gen logiron = log(tpigiron/1000)
gen age = year - birth
gen m = logiron

gen beta_l = .
gen beta_k = .
gen beta_m = .
gen beta_a = .

append using beta_steel_link.dta

replace beta_l = beta_l[_N]
replace beta_k = beta_k[_N]
replace beta_m = beta_m[_N]
replace beta_a = beta_a[_N]
drop if _n == _N

/*generate tfp estimates*/
gen tfp = y - beta_l*l - beta_k*k - beta_m*m - beta_a*age

*trim outliers
drop if labor < 1 | labor > 500000

*normalized tfp 
egen mean_tfp = mean(tfp)
replace tfp = tfp - mean_tfp
gen w_det_steel = exp(beta_l*l + beta_k*k + beta_m*m + beta_a*age)

keep plant firm time year tfp w_det_steel k l

gen t = string(time)
gen fid = plant + t

merge m:1 plant using steel_link.dta,keep(match) /*add facility identifier*/
drop _merge

gen fid_iron_steel = firm + t + steel_link /*stack information at firm_linkage_time level, that is at the production chain level; to make sure that every firm every link of each period only has one observation*/
gen firm_iron_steel = firm + steel_link

/*create weighted information*/
bysort fid_iron_steel: egen det_steel = sum(w_det_steel)
bysort fid_iron_steel: gen nb_steel = _N 

drop if tfp == . | k == . | l == . /*otherwise these missing values will be automatically translated as 0*/

replace tfp = tfp*w_det_steel/det_steel
bysort fid_iron_steel: egen tfp_steel = sum(tfp) /*logtfp weighted by deterministic components*/
replace k = k*w_det_steel/det_steel
bysort fid_iron_steel: egen k_steel = sum(k)
replace l = l*w_det_steel/det_steel
bysort fid_iron_steel: egen l_steel = sum(l)

bysort fid_iron_steel: gen structure = _n /*keep one observation for one chain*/
keep if structure == 1 

keep fid_iron_steel tfp_steel l_steel k_steel nb_steel 

save steel_tfp_lmk_link_iron.dta, replace

********************************************************************************
/*Ownership analysis*/
use steel_plant_pf.dta, clear

keep plant firm time production number size owner region second labor 

gen t = string(time)
gen fid = plant + t
drop t 

*trim outliers
drop if labor < 1 | labor > 500000

merge 1:1 fid using steel_tfp_lmk_link.dta
drop _merge

gen logtfp = log(tfp_steel)

drop if logtfp > 2 | logtfp <= -2 

gen logsize = log(size)
encode owner, gen(ownership)
gen cluster1 = equipment

/*Generate estimates in Table 5 column (7) and (8)*/
reg logtfp i.ownership i.time
outreg2 using steel_owner, keep(i.ownership) addtext(Time FE, YES) tex replace 

reg logtfp i.ownership logsize i.time, cluster(cluster1) 
outreg2 using steel_owner, keep(i.ownership logsize) addtext(Time FE, YES) tex append

/*generate estimates in Table 7 Column (3)*/
replace second = 100 if second > 100

reg second i.ownership i.time, cluster(cluster1) 
outreg2 using quality, keep(i.ownership) addtext(Time FE, YES) cttop(Steel Making) tex append

/*generate estimates in Table 8 Column (7) and (8)*/
rename second quality 
reg logtfp i.ownership quality i.time
outreg2 using quality_stage, keep(i.ownership quality) addtext(Time FE, YES) cttop(Steel Making) tex append 

reg logtfp i.ownership logsize quality i.time
outreg2 using quality_stage, keep(i.ownership logsize quality) addtext(Time FE, YES) cttop(Steel Making) tex append


********************************
*Estimation for pig-iron making*
********************************
clear all
clear matrix
clear mata

set more off

local path = "...folder name"

cd "`path'"

local dlwcprogram = "acf_mata_iron.do"

do `dlwcprogram'

use iron_plant_pf.dta, clear

merge m:1 plant using iron_link.dta /*Add facility identifier*/
drop _merge

gen tid = string(time)
gen fid_iron_steel = firm + tid + iron_steel_link

**merge with downstream steel information 
merge m:1 fid_iron_steel using steel_tfp_lmk_link_iron.dta 

**start estimation
drop if labor < 1
/*generate log variables*/
gen y = log(production)
gen l = log(labor)
gen k = log(size)

gen logpure = log((tore/1000)*(quality/100))
gen logenergy = log(tenergy/1000)
gen age = year - birth
gen m = logpure
gen p = logenergy /*proxy variable*/

*---------------creating variables---------------------------------------------*
* higher order terms on inputs k l a p

forvalues ll = 0(1)5{
         local kmax = 5 - `ll'
		 forvalues kk = 0(1)`kmax'{
		      local amax = 5 - `ll' - `kk'
			  forvalues aa = 0(1)`amax'{
			       local mmax = 5 - `ll' - `kk' - `aa'
				   forvalues mm = 0(1)`mmax'{
				    local pmax = 5 - `ll' - `kk' - `mm' - `aa'
					forvalues pp = 0(1)`pmax'{
				    gen poly`ll'`kk'`mm'`aa'`pp' = (l^`ll')*(k^`kk')*(m^`mm')*(age^`aa')*(p^`pp')
				     }
				   }
			  }
		 }
}

*------FIRST STAGE USING EXP AS INPUT  ----------------------------------------*
local regphi = "poly* i.t i.owner i.region"

xi: reg y `regphi' tfp_steel l_steel k_steel nb_steel 
predict phi
predict epsilon, res
label var phi "phi_it 
label var epsilon "measurement error first stage


local regx = "l k m age i.t i.owner i.region"
xi: reg y `regx' if !missing(p)&!missing(tfp_steel)&!missing(l_steel)&!missing(k_steel)&!missing(nb_steel)
for any l k m: gen OLSX=_b[X]
gen OLSConst=_b[_c]	
gen OLSa = _b[age]

*---------generate esimates in Table 4 Column (3)------------------------------*
local regx = "l k m age i.t i.owner i.region" 

xi: reg y `regx' if !missing(p)&!missing(tfp_steel)&!missing(l_steel)&!missing(k_steel)&!missing(nb_steel) 
outreg2 using iron_ols_lmk_link, keep(l k m age) nocons addtext(Ownership FE,YES, Province FE,YES, Time FE, YES,Firm FE, YES) tex replace
*------------------------------------------------------------------------------*/
xtset id time, monthly
gen phi_lag=L.phi
gen l_lag=L.l
gen k_lag=L.k
gen age_lag = L.age
gen m_lag = L.m
sort id t
gen const=1
drop if y==.
drop if l_lag==.
drop if l == .
drop if k==.
drop if m ==.
drop if m_lag == .
drop if phi==.
drop if phi_lag==.
drop if age ==.

*-------------------------------start search-----------------------------------*

*try ols initial value first
for any l k m: gen initialX=OLSX
gen initiala = OLSa
gen initialx = sqrt(initiall/(1-initiall))
gen initialz = sqrt(initialk/(1-initialk))

dlwc

matrix result = (beta_dlw[1,1]^2/(1+beta_dlw[1,1]^2),beta_dlw[1,2]^2/(1+beta_dlw[1,2]^2),beta_dlw[1,3],beta_dlw[1,4],beta_dlw[1,5],initiall[1],initialk[1],initialm[1])

putexcel A1 = mat(result) using acf_iron_lkm_link, sheet("energy") modify


local n = 2
local iter = 101

set seed 12345 

while `n' < = `iter'{

gen u = (1.2 - 0.8)*runiform() + 0.8 /*search around 80% to 120% of ols coefficients*/

forvalues i = 1(1)4{
gen u`i' = u[`i']
}

replace initiall = OLSl*u1
replace initialx = sqrt(initiall/(1-initiall))
replace initialk = OLSk*u2
replace initialz = sqrt(initialk/(1-initialk))
replace initialm = OLSm*u3
replace initiala = OLSa*u4


dlwc

matrix result = (beta_dlw[1,1]^2/(1+beta_dlw[1,1]^2),beta_dlw[1,2]^2/(1+beta_dlw[1,2]^2),beta_dlw[1,3],beta_dlw[1,4],beta_dlw[1,5],initiall[1],initialk[1],initialm[1])


putexcel A`n' = mat(result) using acf_iron_lkm_link, sheet("energy") modify

matrix drop result
drop u u1-u4

local n = `n' + 1
}

*-------------------generate estimates in Table 4 Column (4)-------------------*
import excel "...folder name\acf_iron_lkm_link.xlsx", sheet("energy") clear
sort E
keep if _n == 1
rename A beta_l
rename B beta_k
rename C beta_m
rename D beta_a
drop E
save beta_iron_link.dta, replace
export excel using iron_acf_lkm_link.xlsx, sheet("energy") replace

/*generate tfp estimates*/
use iron_plant_pf.dta, clear

/*generate log variables*/
gen y = log(production)
gen l = log(labor)
gen k = log(size)
gen logpure = log((tore/1000)*(quality/100))
gen age = year - birth
gen m = logpure

gen beta_l = .
gen beta_k = .
gen beta_m = .
gen beta_a = .

append using beta_iron_link.dta

replace beta_l = beta_l[_N]
replace beta_k = beta_k[_N]
replace beta_m = beta_m[_N]
replace beta_a = beta_a[_N]
drop if _n == _N

/*generate tfp estimates*/
gen tfp = y - beta_l*l - beta_k*k - beta_m*m - beta_a*age
drop if labor < 1
egen mean_tfp = mean(tfp)
replace tfp = exp(tfp - mean_tfp)

/*generate deterministic part*/
gen w_det_iron = exp(beta_l*l + beta_k*k + beta_m*m + beta_a*age)

keep plant firm time tfp w_det_iron
rename tfp tfp_iron

gen t = string(time)
gen fid = plant + t
drop t

save iron_tfp_lmk_link.dta, replace

/*generate controls for upstream stage sintering using linkage link_sinter_iron*/
use iron_plant_pf.dta, clear

merge m:1 plant using iron_link.dta /*Add facility identifier*/
drop _merge

gen tid = string(time)
gen fid_iron_steel = firm + tid + iron_steel_link

**merge with downstream steel information 
merge m:1 fid_iron_steel using steel_tfp_lmk_link_iron.dta /*we lose observations here because of missing data in steel making; we miss information on data*/
drop _merge

/*generate log variables*/
gen y = log(production)
gen l = log(labor)
gen k = log(size)
gen logpure = log((tore/1000)*(quality/100))
gen age = year - birth
gen m = logpure

gen beta_l = .
gen beta_k = .
gen beta_m = .
gen beta_a = .

append using beta_iron_link.dta

replace beta_l = beta_l[_N]
replace beta_k = beta_k[_N]
replace beta_m = beta_m[_N]
replace beta_a = beta_a[_N]
drop if _n == _N

/*generate tfp estimates*/
gen tfp = y - beta_l*l - beta_k*k - beta_m*m - beta_a*age
drop if labor < 1
egen mean_tfp = mean(tfp)
replace tfp = tfp - mean_tfp /*keep logtfp here*/

/*generate deterministic part*/
gen w_det_iron = exp(beta_l*l + beta_k*k + beta_m*m + beta_a*age)

*generate controls for sintering 
gen fid_sinter_iron = firm + tid + sinter_iron_link

/*create weighted information*/
bysort fid_sinter_iron: egen det_iron = sum(w_det_iron)
bysort fid_sinter_iron: gen nb_iron = _N

drop if tfp == . | k == . | l == . | tfp_steel == . | k_steel == . | l_steel == . | nb_steel == . /*drop observations with missing information otherwise will be automatically translated into 0*/

foreach var of varlist tfp k l tfp_steel k_steel l_steel{
replace `var' =`var'*w_det_iron/det_iron
bysort fid_sinter_iron: egen `var'_iron = sum(`var') /*logtfp weighted by deterministic components*/
}

bysort fid_sinter_iron: egen nb_steel_iron = sum(nb_steel) /*number of equipment don't need weight, simply the total number to reflect the structure*/

bysort fid_sinter_iron: gen structure = _n
keep if structure == 1 

keep fid_sinter_iron tfp_iron l_iron k_iron tfp_steel_iron l_steel_iron k_steel_iron nb_iron nb_steel_iron 

save iron_tfp_lmk_link_sinter.dta, replace

/*Start ownership analysis*/
use iron_plant_pf.dta, clear

keep plant firm time year month production number size owner region labor grade1 

gen t = string(time)
gen fid = plant + t
drop t 

drop if labor < 1

merge 1:1 fid using iron_tfp_lmk_link.dta
drop _merge

gen logtfp = log(tfp_iron)

drop if logtfp > 4 | logtfp < -4 

gen logsize = log(size)
encode owner, gen(ownership)
encode plant, gen(equipment)
gen cluster1 = equipment

/*generate estimates in Table 5 Column (5) and (6)*/  
reg logtfp i.ownership i.time
outreg2 using iron_owner, keep(i.ownership) addtext(Time FE, YES) tex replace 

reg logtfp i.ownership logsize i.time
outreg2 using iron_owner, keep(i.ownership logsize) addtext(Time FE, YES) tex append

/*generate estimates in Table 7 Column (2)*/
replace grade1 = 100 if grade1 > 100
reg grade1 i.ownership i.time,cluster(cluster1)
outreg2 using quality, keep(i.ownership) addtext(Time FE, YES,Clustered by equipment, YES) cttop(Iron Making) tex append

/*generate estimates in Table 8 Column (5) and (6)*/
rename grade1 quality 

reg logtfp i.ownership quality i.time
outreg2 using quality_stage, keep(i.ownership quality) addtext(Time FE, YES) cttop(Pig-iron Making) tex append 

reg logtfp i.ownership logsize quality i.time
outreg2 using quality_stage, keep(i.ownership logsize quality) addtext(Time FE, YES) cttop(Pig-iron Making) tex append 


****************************
*Estimation for  sintering *
****************************

clear all
clear matrix
clear mata

set more off

local path = "...folder name"

cd "`path'"


local dlwcprogram = "acf_mata_sinter.do"

do `dlwcprogram'

use sinter_plant_pf.dta, clear

merge m:1 plant using sinter_link.dta /*Add facility identifier*/
drop _merge

gen tid = string(time)
gen fid_sinter_iron = firm + tid + sinter_link

**merge with downstream steel information 
merge m:1 fid_sinter_iron using iron_tfp_lmk_link_sinter.dta /*we lose observations here because of missing data in steel making; we miss information on data*/

/*generate log variables*/
gen y = log(production*grade/100) 
gen l = log(labor)
gen k = log(size)
gen age = year - birth
gen logenergy = log(tenergy/1000)
gen p = logenergy /*proxy variable*/
gen p1 = log(energy)

*drop outlier
drop if p1 < 0 | p1 > 6.9 
drop if labor > 10000 | labor < 1 
drop if grade > 100 
drop if firm == "首钢" & (size == 65 | size == 200)

* higher order terms on inputs k l a p

forvalues ll = 0(1)4{
         local kmax = 4 - `ll'
		 forvalues kk = 0(1)`kmax'{
		      local amax = 4 - `ll' - `kk'
			  forvalues aa = 0(1)`amax'{
			       local pmax = 4 - `ll' - `kk' - `aa'
				   forvalues pp = 0(1)`pmax'{
				   gen poly`ll'`kk'`aa'`pp' = (l^`ll')*(k^`kk')*(age^`aa')*(p^`pp')
				   }
			  }
		 }
}

*------FIRST STAGE USING EXP AS INPUT  ----------------------------------------*
local regphi = "poly* i.t i.owner i.region"

xi: reg y `regphi' tfp_steel_iron tfp_iron l_steel_iron l_iron k_steel_iron k_iron nb_iron nb_steel_iron 
predict phi
predict epsilon, res
label var phi "phi_it 
label var epsilon "measurement error first stage

local regx = "l k age i.t i.owner i.region"

xi: reg y `regx' if !missing(p)&!missing(tfp_iron)&!missing(tfp_steel_iron)&!missing(l_iron)&!missing(l_steel_iron)&!missing(k_iron)&!missing(k_steel_iron) 
for any l k: gen OLSX=_b[X]
gen OLSConst=_b[_c]	
gen OLSa = _b[age]

*----------generate estimates in Table 4 Column (1)-----------------------------------------------*
local regx = "l k age i.t i.owner i.region"

xi: reg y `regx' if !missing(p)&!missing(tfp_iron)&!missing(tfp_steel_iron)&!missing(l_iron)&!missing(l_steel_iron)&!missing(k_iron)&!missing(k_steel_iron) 
outreg2 using sinter_ols_lmk_link, keep(l k age) nocons addtext(Ownership FE,YES, Province FE,YES, Time FE, YES) tex replace
*------------------------------------------------------------------------------*
xtset id time, monthly
gen phi_lag=L.phi
gen l_lag=L.l
gen k_lag=L.k
gen age_lag = L.age

sort id t
gen const=1
drop if y==.
drop if l_lag==.
drop if l == .
drop if k==.

drop if phi==.
drop if phi_lag==.
drop if age ==.

*-------------------------------start search-----------------------------------*

*try ols initial value first
for any l k: gen initialX=OLSX
gen initiala = OLSa
gen initialx = sqrt(initiall/(1-initiall))
gen initialz = sqrt(initialk/(1-initialk))

dlwc

matrix result = (beta_dlw[1,1]^2/(1+beta_dlw[1,1]^2),beta_dlw[1,2]^2/(1+beta_dlw[1,2]^2),beta_dlw[1,3],beta_dlw[1,4],initiall[1],initialk[1])

putexcel A1 = mat(result) using acf_sinter_link, sheet("y2_energy") modify

local n = 2
local iter = 101

set seed 12345 

while `n' < = `iter'{

gen u = (1.2 - 0.8)*runiform() + 0.8 /*search around 80% to 120% of ols coefficients*/

forvalues i = 1(1)3{
gen u`i' = u[`i']
}                                                                                                                                                                                                                                      

replace initiall = OLSl*u1
replace initialx = sqrt(initiall/(1-initiall))
replace initialk = OLSk*u2
replace initialz = sqrt(initialk/(1-initialk))
replace initiala = OLSa*u3


dlwc

matrix result = (beta_dlw[1,1]^2/(1+beta_dlw[1,1]^2),beta_dlw[1,2]^2/(1+beta_dlw[1,2]^2),beta_dlw[1,3],beta_dlw[1,4],initiall[1],initialk[1])


putexcel A`n' = mat(result) using acf_sinter_link, sheet("y2_energy") modify

matrix drop result
drop u u1-u3

local n = `n' + 1
}

*-------------generate estimates in Table 4 Column (2)-------------------------*
import excel "...folder name\acf_sinter_link.xlsx", sheet("y2_energy") clear
sort D
keep if _n == 1
rename A beta_l
rename B beta_k
rename C beta_a
drop D
save beta_sinter_link.dta, replace
export excel using sinter_acf_link.xlsx, sheet("y2_energy") replace


/*generate tfp estimates*/
use sinter_plant_pf.dta, clear

/*generate log variables*/
gen y = log(production*grade/100)
gen l = log(labor)
gen k = log(size)
gen age = year - birth

gen beta_l = .
gen beta_k = .
gen beta_a = .

append using beta_sinter_link.dta

replace beta_l = beta_l[_N]
replace beta_k = beta_k[_N]
replace beta_a = beta_a[_N]
drop if _n == _N

/*generate tfp estimates*/
gen tfp = y - beta_l*l - beta_k*k - beta_a*age

*trim outliers
drop if labor < 1 | labor > 10000

egen mean_tfp = mean(tfp)
replace tfp = exp(tfp - mean_tfp)

/*generate deterministic part*/
gen w_det_sinter = exp(beta_l*l + beta_k*k + beta_a*age)

keep plant firm time tfp w_det_sinter
rename tfp tfp_sinter

gen t = string(time)
gen fid = plant + t
drop t

save sinter_tfp_lmk_link.dta, replace

/*Start ownership analysis*/
use sinter_plant_pf.dta, clear

keep id firm plant time year production number size owner region labor grade_stability

gen t = string(time)
gen fid = plant + t
drop t 

drop if labor < 1 | labor > 10000

merge 1:1 fid using sinter_tfp_lmk_link.dta
drop _merge

gen logtfp = log(tfp_sinter)

drop if logtfp > 5 

gen logsize = log(size)
encode owner, gen(ownership)
encode plant, gen(equipment)
gen cluster1 = equipment


/*generate estimates in Table 5 Column (3) and (4) */ 
reg logtfp i.ownership i.time, cluster(cluster1) 
outreg2 using sinter_owner, keep(i.ownership) addtext(Time FE, YES) tex replace 

reg logtfp i.ownership logsize i.time, cluster(cluster1) 
outreg2 using sinter_owner, keep(i.ownership logsize) addtext(Time FE) tex append

/*generate estimates in Table 7 Column (1)  */
replace grade_stability = 100 if grade_stability>100

reg grade_stability i.ownership i.time, cluster(cluster1)
outreg2 using quality, keep(i.ownership) addtext(Time FE, YES,Clustered by equipment, YES) cttop(Sintering) tex replace 

/*generate estimates in Table 8 Column (3) and (4)*/
rename grade_stability quality 

reg logtfp i.ownership quality i.time
outreg2 using quality_stage, keep(i.ownership quality) addtext(Time FE, YES) cttop(Sintering) tex replace 

reg logtfp i.ownership logsize quality i.time
outreg2 using quality_stage, keep(i.ownership logsize quality) addtext(Time FE, YES) cttop(Sintering) tex append 












